package com.venutech.gongan.api;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.security.LoginUser;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.venutech.gongan.entity.TbWorkOrder;
import com.venutech.gongan.service.ITbWorkOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 巡检Controller
 *
 * @author linxb
 * @date 2023-09-18
 */
@Api(value = "巡检工单管理", tags = "巡检管理")
@RestController
@RequestMapping("/gongan/tbWorkOrder")
public class TbWorkOrderController extends BaseController {


    @Autowired
    private ITbWorkOrderService tbWorkOrderService;


    /**
     * 查询巡检列表
     */
    @ApiOperation("查询巡检管理列表分页")
    @GetMapping("/list")
    @ResponseBody
    public TableDataInfo list(TbWorkOrder tbWorkOrder) {
        startPage();
        LoginUser loginUser = SecurityUtils.getLoginUser();
        tbWorkOrder.setWkSqUserId(loginUser.getUserId());
        List<TbWorkOrder> list = tbWorkOrderService.selectTbWorkOrderList(tbWorkOrder);
        return getDataTable(list);
    }

    /**
     * 查询巡检列表
     */
    @ApiOperation("查询巡检管理列表")
    @GetMapping("/getTbWorkOrder")
    @ResponseBody
    public AjaxResult getTbWorkOrder(TbWorkOrder tbWorkOrder) {
        List<TbWorkOrder> list = tbWorkOrderService.selectTbWorkOrderList(tbWorkOrder);
        return AjaxResult.success(list);
    }

    /**
     * 导出巡检列表
     */
//    @PostMapping("/export")
//    @ResponseBody
//    public AjaxResult export(@RequestBody TbWorkOrder tbWorkOrder)
//    {
//        List<TbWorkOrder> list = tbWorkOrderService.selectTbWorkOrderList(tbWorkOrder);
//        ExcelUtil<TbWorkOrder> util = new ExcelUtil<TbWorkOrder>(tbWorkOrder.class);
//        return util.exportExcel(list, "巡检数据");
//    }


    /**
     * 新增保存巡检
     */
    @ApiOperation("新增保存巡检")
    @PostMapping("/addTbWorkOrder")
    @ResponseBody
    public AjaxResult addSave(@RequestBody TbWorkOrder tbWorkOrder) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        tbWorkOrder.setWkSqUserId(loginUser.getUserId());
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date(System.currentTimeMillis());
        System.out.println(formatter.format(date));
        tbWorkOrder.setWkSqTime(date);
        return toAjax(tbWorkOrderService.insertTbWorkOrder(tbWorkOrder));
    }


    /**
     * 修改保存巡检
     */
    @ApiOperation("修改保存巡检")
    @PostMapping("/editTbWorkOrder")
    @ResponseBody
    public AjaxResult editSave(@RequestBody TbWorkOrder tbWorkOrder) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        TbWorkOrder opWork = new TbWorkOrder();
        //BeanUtils.copyProperties(tbOpWorkOrder,opWork);
        opWork.setWkSqUserId(loginUser.getUserId());
        List<TbWorkOrder> opWorkList = tbWorkOrderService.selectTbWorkOrderList(opWork);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date(System.currentTimeMillis());
        tbWorkOrder.setWkSqTime(date);
        return toAjax(tbWorkOrderService.updateTbWorkOrder(tbWorkOrder));
    }

    /**
     * 巡检工单审批
     */
    @ApiOperation("巡检工单审批")
    @PostMapping("/approveTbWorkOrder")
    @ResponseBody
    public AjaxResult approveTbWorkOrder(@RequestBody TbWorkOrder tbWorkOrder) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        TbWorkOrder opWork = new TbWorkOrder();
        //BeanUtils.copyProperties(tbOpWorkOrder,opWork);
        opWork.setWkSpUserId(loginUser.getUserId());
        List<TbWorkOrder> opWorkList = tbWorkOrderService.selectTbWorkOrderList(opWork);
        if (opWorkList.size() > 0) {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = new Date(System.currentTimeMillis());
            tbWorkOrder.setWkSpTime(date);
            return toAjax(tbWorkOrderService.approveTbWorkOrder(tbWorkOrder));
        } else {
            return AjaxResult.error("该工单只能由指定审批人审批");
        }

    }

    /**
     * 巡检工单审批
     */
    @ApiOperation("故障工单审批")
    @PostMapping("/approveFaultOrder")
    @ResponseBody
    public AjaxResult approveFaultOrder(@RequestBody TbWorkOrder tbWorkOrder) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date(System.currentTimeMillis());
        tbWorkOrder.setWkSpTime(date);
        tbWorkOrder.setWkSpUserId(loginUser.getUserId());
        return toAjax(tbWorkOrderService.approveFaultOrder(tbWorkOrder));

    }

    /**
     * 删除巡检
     */

//    @PostMapping( "/remove")
//    @ResponseBody
//    public AjaxResult remove(String ids)
//    {
//        return toAjax(TbWorkOrderService.deleteTbWorkOrderByWkIds(ids));
//    }

    /**
     * 删除巡检
     */
    @ApiOperation("删除巡检")
    @GetMapping("/deleteTbWorkOrderByWkId/{id}")
    @ResponseBody
    public AjaxResult deleteTbOpWorkByOpWorkId(@PathVariable Long id) {
        return toAjax(tbWorkOrderService.deleteTbWorkOrderByWkId(id));
    }
}
